// HSS.Net.Mail.DataCommand.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       DataCommand.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Net.Mail
{
	#region Using Directives
	using System;
	using System.Collections.Generic;
	using System.Text;
	using System.Net.Mail;
	#endregion

	#region DataCommand
	/// <summary>
	/// Used to send the body data to an smtp server
	/// </summary>
	static class DataCommand
	{
		static SmtpStatusCode ResponseCode = SmtpStatusCode.GeneralFailure;
		static string Response = string.Empty;

		internal static void Send(SmtpConnection smtpConn, string data)
		{

			smtpConn.Buffer.Append(SmtpCommands.Data);
			smtpConn.Send();
			smtpConn.GetResponse(out ResponseCode, out Response);

			if (ResponseCode != SmtpStatusCode.StartMailInput)
				throw new SmtpException(ResponseCode, Response);

			DataCommand.PrepareCommand(smtpConn, data);

			smtpConn.Send();

			smtpConn.GetResponse(out ResponseCode, out Response);

			DataCommand.CheckResponse(ResponseCode, Response);

		}
		private static void PrepareCommand(SmtpConnection smtpConn, string data)
		{
			ResponseCode = SmtpStatusCode.GeneralFailure;
			Response = string.Empty;

			smtpConn.Buffer.Append(data);
			smtpConn.Buffer.Append(SmtpCommands.DataStop);
		}
		private static void CheckResponse(SmtpStatusCode code, string response)
		{
			if (code != SmtpStatusCode.Ok)
			{
				throw new SmtpException(code, response);
			}
		}
	}
	#endregion
}